package com.biostime.dingdingbox.modules.db.api;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.biostime.dingdingbox.common.api.FastResponse;
import com.biostime.dingdingbox.common.api.ListResponse;
import com.biostime.dingdingbox.common.api.ObjResponse;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.db.dto.CommentDto;
import com.biostime.dingdingbox.modules.db.entity.Comment;
import com.biostime.dingdingbox.modules.db.entity.Member;
import com.biostime.dingdingbox.modules.db.service.CommentService;
import com.biostime.dingdingbox.modules.db.service.HttpRequestService;
import com.biostime.dingdingbox.modules.db.service.MemberService;
import com.biostime.dingdingbox.modules.sys.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 * 评论API
 * @author chenweicheng
 * @version 2016-06-28
 */
@RestController
@RequestMapping(value = "${adminPath}/db/api/comment")
public class CommentApi extends BaseController {
		
	@Autowired
	private CommentService commentService;
	
	@Autowired
	private MemberService memberService;
		
	/**
	 * 评论
	 */
	@RequestMapping(value = "comment")
	public ObjResponse<String> comment(String type, String content ,String objectId,String userId , HttpServletRequest request ,
				HttpServletResponse response) {		
		response.setHeader("Access-Control-Allow-Origin", "*");
		userId = memberService.apiGetUserId(userId);
		String message = "评论成功";				
		if(userId != null && !userId.equals("") && type != null && objectId != null && content != null){
			Comment comment = new Comment();
			Member member = memberService.get(userId);
			comment.setCategory(type);
			comment.setObjectId(Integer.parseInt(objectId));
			comment.setContent(content);
			comment.setFromIp(HttpRequestService.getIpAddress(request));  
			comment.setCreateBy(new User("1"));
			comment.setCreateDate(new Date());
			comment.setUpdateBy(new User("1"));
			comment.setUpdateDate(new Date()); 
			comment.setDelFlag("0"); 
			comment.setReplyName(member.getName());
			comment.setParentId(0); 
			comment.setIsReply(0);
			comment.setIsAudit(0); 
			comment.setReplyId(Integer.parseInt(userId));
			commentService.save(comment); 			
		}else{
			message = "评论失败,参数不全";
		}
		return  FastResponse.create().errMsg(message).obj("评论操作");		
	}
	
	/**
	 * 评论列表
	 */
	@RequestMapping(value = "commentList")
	public ListResponse<CommentDto> commentList(String userId,String type , String objectId ,  int pageindex,int pagesize,
			HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		userId = memberService.apiGetUserId(userId);
		List<CommentDto> commentList = new ArrayList<CommentDto>();
		if(userId != null && !userId.equals("")){
				PageHelper.startPage(pageindex, pagesize);
				commentList = commentService.apiGetCommentList(type, objectId);
		}	
		
		PageInfo<CommentDto> page = new PageInfo<CommentDto>(commentList);
		return FastResponse.create().errMsg("获取更多玩法成功").pageIndex(page.getPageNum())
				.pageSize(page.getPageSize()).total((int) page.getTotal()).hasNext(page.isHasNextPage())
				.list(commentList);
		
	}	
		

}
